package org.jeecg.modules.clues.jobs;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.crawler.other.SearchNewsCrawler;
import org.jeecg.modules.clues.entity.Clues;
import org.jeecg.modules.clues.service.ICluesService;
import org.jeecg.modules.crawlerpaper.entity.CrawlerInfo;
import org.jeecg.modules.crawlerpaper.service.ICrawlerInfoService;
import org.jeecg.modules.system.service.ISysDictService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 学习强国爬虫线索
 */
@Slf4j
public class XXQGCluesJob implements Job {

    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改
     */
    private String parameter;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    @Autowired
    private ICluesService cluesService;

    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<String> keywords = cluesService.getAllKeyword();
        if(!CollectionUtils.isEmpty(keywords)){
            List<Clues> clueList = getXXQGNews(keywords, Integer.valueOf(parameter));
            cluesService.saveOrUpdateBatch(clueList);
        }
    }



    /**
     * 获取学习强国新闻
     * @param keywords
     * @param page
     * @return
     * @throws Exception
     */
    public  static List<Clues>  getXXQGNews(List<String > keywords, Integer page) throws Exception {
        List<Clues>  clueList = new ArrayList<>();
        String xxggUrl = "https://static.xuexi.cn/search/online/index.html";
        if(!CollectionUtils.isEmpty(keywords)){
            for(int i =1;i<=page;i++){
                for (String keyword : keywords) {
                    List<Clues>  iCrawlerInfoList = getXXQGPageResponse(xxggUrl+"?page="+i+"&query="+keyword,keyword);
                    if(!CollectionUtils.isEmpty(iCrawlerInfoList)){
                        clueList.addAll(iCrawlerInfoList);
                    }
                }
            }
        }
        return   clueList ;
    }

    /**
     * 学习强国
     * @param
     * @return
     * @throws Exception
     */
    public static List<Clues> getXXQGPageResponse(String url,String keyword) throws Exception {
        WebClient webClient = getClient();
        Thread.sleep(3000);
        String result =  getHtml( url,  webClient);

        List<Clues> crawlerInfoList = new ArrayList<>();

        Document doc = Jsoup.parse(result);

        Elements elementsByClass = doc.getElementsByClass("c-card");

        for (Element byClass : elementsByClass) {
            Elements children = byClass.children();
            for (Element child : children) {
                if(child.className().equals("rt-kpicture")){
                    Document dataDoc = Jsoup.parse(child.html());
                    Clues clue = new Clues();
                    String href = child.attr("href");
                    clue.setId("xxqg"+href.substring(href.indexOf("item_id")+8,href.length()));
                    clue.setKeyword(keyword);
                    clue.setCluesTitle(dataDoc.getElementsByClass("title").text());
                    clue.setCrawlerUrl(child.attr("href"));
                    clue.setCluesSource(1);
                    clue.setCrawlerSource(1);
                    crawlerInfoList.add(clue);

                }

            }

        }

        webClient.close();

        return crawlerInfoList;
    }




    public  static WebClient  getClient(){
        WebClient  webClient = new WebClient(BrowserVersion.CHROME);

        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setActiveXNative(false);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        webClient.getOptions().setTimeout(30000);
        webClient.setJavaScriptTimeout(30000);
        webClient.getOptions().setRedirectEnabled(true);
        return  webClient;



    }


    public  static String  getHtml(String url, WebClient webClient) throws InterruptedException {
        HtmlPage page = null;
        try {
            webClient.getOptions().setUseInsecureSSL(true);
            page = webClient.getPage(url);
        } catch (Exception e) {
            e.printStackTrace();
            webClient.close();

        }

        //该方法阻塞线程
        webClient.waitForBackgroundJavaScript(30000);
        return   page.asXml();
    }


    public  static String  getHrefId(String  href,String endString){
        String[] split = href.split("/");
        return  split[split.length-1].replace("."+endString,"");
    }


    public static String filenameFilter(String htmlStr) {
        String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
        String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
        String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式

        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); // 过滤script标签

        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); // 过滤style标签

        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); // 过滤html标签

        String str = htmlStr.trim(); // 返回文本字符串
        Pattern FilePattern = Pattern.compile("[\\\\/:*?\"<>|]");
        str = str == null ? null : FilePattern.matcher(str).replaceAll("");
        str = str.replaceAll("\\s*", "").replaceAll("", "");
        str =str.replaceAll("&nbsp;","");
        return str;
    }



}
